<?php

namespace App\Http\Controllers;

use App\Admin;
use App\AppConfig;
use App\Datatables;
use App\Permission;
use App\Role;
use Illuminate\Http\Request;
use Auth;
use Hash;

class SystemController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * 管理员列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function admin()
    {
        return view('system.admins');
    }

    /**
     * 添加管理员
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function adminAdd (Request $request)
    {
        if ($request->isMethod('post')) {
            $result = Admin::CreateAdmin($request);
            return response()->json($result);
        }
    }


    /**
     * 管理员详情
     * @param Request $request
     * @param Admin $admin
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function adminDetail(Request $request, Admin $admin)
    {
        if ($request->isMethod('post')) {
            $result = Admin::ModifyAdmin($admin->id);
            return response()->json($result);
        }
        return view('system.adminsDetail', ['admin' => $admin]);
    }

    /**
     * 删除管理员
     * @param Admin $admin
     * @return array
     */
    public function adminDel(Admin $admin)
    {

        if ($admin->delete()) {
            return ['status' => 1, 'message' => '删除成功'];
        } else {
            return ['status' => 2, 'message' => '删除失败'];
        }
    }

    /**
     * 角色列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function roles(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Datatables::Roleslist());
        }
        return view('system.roles');
    }



    /**
     * 添加角色
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function rolesPublish(Request $request)
    {
        if ($request->isMethod('post')) {
            $result = Role::AddRole();
            return response()->json($result);
        }
    }

    /**
     * 删除角色
     * @param Role $role
     * @return array
     */
    public function rolesDel(Role $role)
    {
        if ($role->delete()) {
            return ['status' => 1, 'message' => '删除成功'];
        } else {
            return ['status' => 2, 'message' => '删除失败'];
        }
    }


    /**
     * //编辑角色
     * @param Role $role
     * @return Role
     */
    public function rolesChk(Role $role)
    {
        $permission = Role::Permissions($role->id);
        $permissions = [];
        if ($permission) {
            foreach ($permission as $data){
                $permissions[] = $data->id;
            }
            $role->permissions = $permissions;
        }

        return $role;
    }

    /**
     * 编辑角色
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function rolesEdit(Request $request)
    {
        if ($request->isMethod('post')) {
            $result = Role::ModifyRole($request->id);
            return response()->json($result);
        }
    }


    /**
     *  //权限
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function permissions(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Datatables::Permissionslist());
        }
        return view('system.permissions');
    }


    /**
     *  //添加权限
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function permissionsPublish(Request $request)
    {
        if ($request->isMethod('post')) {
            $result = Permission::AddPermission();
            return response()->json($result);
        }
    }


    /**
     * 删除权限
     * @param Permission $permission
     * @return array
     */
    public function permissionsDel(Permission $permission)
    {
        if ($permission->delete()) {
            return ['status' => 1, 'message' => '删除成功'];
        } else {
            return ['status' => 2, 'message' => '删除失败'];
        }
    }



    /**
     *  //编辑
     * @param Permission $permission
     * @return Permission
     */
    public function permissionsChk(Permission $permission)
    {
        return $permission;
    }

    /**
     * 修改节点
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function permissionsEdit(Request $request)
    {
        if ($request->isMethod('post')) {
            $result = Permission::ModifyPermission($request->id);
            return response()->json($result);
        }
    }

    /**
     * 修改密码
     * @param Request $request
     * @return array
     */
    public function pwdEidt(Request $request)
    {
        if (!Hash::check($request->oldpwd, Auth::user()->password)) {
            return ['status' => 2, 'message' => '旧密码不正确'];
        }
        if ($request->newpwd !== $request->renewpwd) {
            return ['status' => 2, 'message' => '两次密码不一样'];
        }
        return Admin::PwdEidt($request);
    }


    /**
     * APP配置
     * @param Request $request
     * @return $this|\Illuminate\Http\JsonResponse
     */
    public function appConfig(Request $request)
    {
        $config = AppConfig::find(1);
        if ($request->isMethod('post')) {
            return response()->json(AppConfig::ConfigModify($config, $request));
        }

        return view('system.appConfig')->with(compact('config'));
    }

}